<?php defined('BASEPATH') OR exit('No direct script access allowed');

include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Client.php');
include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Exception.php');

require APPPATH.'/libraries/REST_Controller.php';

class Products extends REST_Controller
{
	private $service;
	public $request;

	function __construct()
	{
		// Construct our parent class
		parent::__construct();
		
		$serviceUrl = '';
		switch ($this->_merchant['region'])
		{
			case 'NA':
				$serviceUrl = "https://mws.amazonservices.com/Products/2011-10-01";
				break;

			case 'CA':
				$serviceUrl = "https://mws.amazonservices.ca/Products/2011-10-01";
				break;

			case 'DE':
				$serviceUrl = "https://mws.amazonservices.de/Products/2011-10-01";
				break;

			case 'FR':
				$serviceUrl = "https://mws.amazonservices.fr/Products/2011-10-01";
				break;

			case 'IT':
				$serviceUrl = "https://mws.amazonservices.it/Products/2011-10-01";
				break;

			case 'UK':
				$serviceUrl = "https://mws.amazonservices.co.uk/Products/2011-10-01";
				break;

			case 'JP':
				$serviceUrl = "https://mws.amazonservices.com.cn/Products/2011-10-01";
				break;

			case 'CN':
				$serviceUrl = "https://mws.amazonservices.com.cn/Products/2011-10-01";
				break;

			default:
				$serviceUrl = "https://mws.amazonservices.com/Products/2011-10-01";
			break;
		}

		$this->service = new MarketplaceWebServiceProducts_Client(
		AWS_ACCESS_KEY_ID,
		AWS_SECRET_ACCESS_KEY,
		APPLICATION_NAME,
		APPLICATION_VERSION,
		array (
                'ServiceURL' => $serviceUrl,
                'ProxyHost' => null,
                'ProxyPort' => -1,
                'MaxErrorRetry' => 3
		)
		);


	}

	function listMatchingProducts_get()
	{

		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/ListMatchingProductsRequest.php');

		$this->request = new MarketplaceWebServiceProducts_Model_ListMatchingProductsRequest();

		$this->request->setSellerId(MERCHANT_ID);

		$params = array('Query' => array('type' => 'string'));
		$this->amazon_mws->set_request( $params, 'get');

		if( !$this->request->issetMarketplaceId() )
			$this->request->setMarketplaceId(MARKETPLACE_ID);

		$this->invokeListMatchingProducts($this->service, $this->request);

	}

	function getServiceStatus_get()
	{

		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetServiceStatusRequest.php');

		$this->request = new MarketplaceWebServiceProducts_Model_GetServiceStatusRequest();
		$this->request->setSellerId(MERCHANT_ID);
		
		
		$this->invokeGetServiceStatus($this->service, $this->request);
	}

	function getProductCategoriesForSKU_get()
	{

		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetProductCategoriesForSKURequest.php');

		$this->request = new MarketplaceWebServiceProducts_Model_GetProductCategoriesForSKURequest();
		$this->request->setSellerId(MERCHANT_ID);

		$params = array('SellerSKU' => array('type' => 'string'));
		$this->amazon_mws->set_request( $params, 'get');//31414-101-3p

		if( !$this->request->issetMarketplaceId() )
			$this->request->setMarketplaceId(MARKETPLACE_ID);
		
		$this->invokeGetProductCategoriesForSKU($this->service, $this->request);
	}

	function getProductCategoriesForASIN_get()
	{
		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetProductCategoriesForASINRequest.php');

		$this->request = new MarketplaceWebServiceProducts_Model_GetProductCategoriesForASINRequest();
		$this->request->setSellerId(MERCHANT_ID);

		$params = array('ASIN' => array('type' => 'string'));//B00P481KG6
		$this->amazon_mws->set_request( $params, 'get');

		if( !$this->request->issetMarketplaceId() )
			$this->request->setMarketplaceId(MARKETPLACE_ID);
		

		$this->invokeGetProductCategoriesForASIN($this->service, $this->request);
	}

	function getMyPriceForSKU_get()
	{
		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetMyPriceForSKURequest.php');

		$this->request = new MarketplaceWebServiceProducts_Model_GetMyPriceForSKURequest();
		$this->request->setSellerId(MERCHANT_ID);

		$params = array('SellerSKUList' => array('type' => 'class'));
		$this->amazon_mws->set_request( $params, 'get'); //20 sku's only allowed

		if( !$this->request->issetMarketplaceId() )
			$this->request->setMarketplaceId(MARKETPLACE_ID);
		
		$this->invokeGetMyPriceForSKU($this->service, $this->request);
	}

	function getMyPriceForASIN_get()
	{
		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetMyPriceForASINRequest.php');

		$this->request = new MarketplaceWebServiceProducts_Model_GetMyPriceForASINRequest();
		$this->request->setSellerId(MERCHANT_ID);

		$params = array('ASINList' => array('type' => 'class'));
		$this->amazon_mws->set_request( $params, 'get'); //20 ASIN's only allowed

		if( !$this->request->issetMarketplaceId() )
		$this->request->setMarketplaceId(MARKETPLACE_ID);
		
		$this->invokeGetMyPriceForASIN($this->service, $this->request);
	}
	
	function getMatchingProduct_get()
	{
		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetMatchingProductRequest.php');

		$this->request = new MarketplaceWebServiceProducts_Model_GetMatchingProductRequest();
		$this->request->setSellerId(MERCHANT_ID);

		$params = array('ASINList' => array('type' => 'class'));
		$this->amazon_mws->set_request( $params, 'get'); //20 ASIN's only allowed

		if( !$this->request->issetMarketplaceId() )
		$this->request->setMarketplaceId(MARKETPLACE_ID);
		
		$this->invokeGetMatchingProduct($this->service, $this->request);
	}

	function getMatchingProductForId_get()
	{
		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetMatchingProductForIdRequest.php');

		$this->request = new MarketplaceWebServiceProducts_Model_GetMatchingProductForIdRequest();
		$this->request->setSellerId(MERCHANT_ID);

		 
		$params = array('IdType' => array('type' => 'string'),  //for example ASIN,GCID,SellerSKU,UPC,EAN,ISBN,JAN
                        'IdList' => array('type' => 'class'));
		$this->amazon_mws->set_request( $params, 'get'); //20 ASIN's only allowed

		if( !$this->request->issetMarketplaceId() )
		$this->request->setMarketplaceId(MARKETPLACE_ID);
		
		$this->invokeGetMatchingProductForId($this->service, $this->request);
	}

	function getLowestOfferListingsForSKU_get()
	{
		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetLowestOfferListingsForSKURequest.php');

		$this->request = new MarketplaceWebServiceProducts_Model_GetLowestOfferListingsForSKURequest();
		$this->request->setSellerId(MERCHANT_ID);

		$params = array('ItemCondition' => array('type' => 'string'),    //for Any,New,Used,Collectible,Refurbished,Club ....the default:Any
                        'ExcludeMe' => array('type' => 'boolean'),          
                        'SellerSKUList' => array('type' => 'class'));
		/*
		 For Exclude parameter:

		True
		– Your own offer listings are
		excluded from the offer listings that
		are returned.
		•
		False
		– Your own offer listings are
		included in the offer listings that
		are returned.

		Default:
		False
		*/

		$this->amazon_mws->set_request( $params, 'get'); //20 ASIN's only allowed

		// object or array of parameters
		if( !$this->request->issetMarketplaceId() )
		$this->request->setMarketplaceId(MARKETPLACE_ID);

		$this->invokeGetLowestOfferListingsForSKU($this->service, $this->request);
	}
	
	function getLowestOfferListingsForASIN_get()
	{
		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetLowestOfferListingsForASINRequest.php');
	
		$this->request = new MarketplaceWebServiceProducts_Model_GetLowestOfferListingsForASINRequest();
		$this->request->setSellerId(MERCHANT_ID);
	
		$params = array('ItemCondition' => array('type' => 'string'),    //for Any,New,Used,Collectible,Refurbished,Club ....the default:Any
	                        'ExcludeMe' => array('type' => 'boolean'),          
	                        'ASINList' => array('type' => 'class'));
		/*
		 For Exclude parameter:
	
		True
		– Your own offer listings are
		excluded from the offer listings that
		are returned.
		•
		False
		– Your own offer listings are
		included in the offer listings that
		are returned.
	
		Default:
		False
		*/
	
		$this->amazon_mws->set_request( $params, 'get'); //20 ASIN's only allowed
	
		// object or array of parameters
		if( !$this->request->issetMarketplaceId() )
		$this->request->setMarketplaceId(MARKETPLACE_ID);
	
		$this->invokeGetLowestOfferListingsForASIN($this->service, $this->request);
	}

	function getCompetitivePricingForSKU_get()
	{
		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetCompetitivePricingForSKURequest.php');
	
		$this->request = new MarketplaceWebServiceProducts_Model_GetCompetitivePricingForSKURequest();
		$this->request->setSellerId(MERCHANT_ID);
	
		$params = array('SellerSKUList' => array('type' => 'class'));
		
		$this->amazon_mws->set_request( $params, 'get'); //20 ASIN's only allowed
	
		if( !$this->request->issetMarketplaceId() )
			$this->request->setMarketplaceId(MARKETPLACE_ID);
	
		$this->invokeGetCompetitivePricingForSKU($this->service, $this->request);
	}
	
	function getCompetitivePricingForASIN_get()
	{
		//include required libraries
		include_once(APPPATH.'libraries/MarketplaceWebServiceProducts/Model/GetCompetitivePricingForASINRequest.php');
	
		$this->request = new MarketplaceWebServiceProducts_Model_GetCompetitivePricingForASINRequest();
		$this->request->setSellerId(MERCHANT_ID);
	
		$params = array('ASINList' => array('type' => 'class'));
	
		$this->amazon_mws->set_request( $params, 'get'); //20 ASIN's only allowed
	
		if( !$this->request->issetMarketplaceId() )
			$this->request->setMarketplaceId(MARKETPLACE_ID);
	
		$this->invokeGetCompetitivePricingForASIN($this->service, $this->request);
	}
	
	
	
	
	
	
	
	
	
	function invokeListMatchingProducts(MarketplaceWebServiceProducts_Interface $service, $request)
	{

		try
		{
			$response = $service->ListMatchingProducts($request);

			$this->amazon_mws->format_output($response, 'ListMatchingProductsResult|Products|Product', 'Products');

			$this->amazon_mws->show_ouput(TRUE);


		} catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}

	}

	function invokeGetServiceStatus(MarketplaceWebServiceProducts_Interface $service, $request)
	{

		try
		{
			$response = $service->GetServiceStatus($request);

			$this->amazon_mws->format_output($response, 'GetServiceStatusResult', 'ServiceStatus');

			$this->amazon_mws->show_ouput(TRUE);


		} catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}

	}

	function invokeGetProductCategoriesForSKU(MarketplaceWebServiceProducts_Interface $service, $request)
	{
		try
		{
			$response = $service->GetProductCategoriesForSKU($request);

			$this->amazon_mws->format_output($response, 'GetProductCategoriesForSKUResult|Self', 'ProductCategoriesForSKU');

			$this->amazon_mws->show_ouput(TRUE);


		} catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}
		 
	}

	function invokeGetProductCategoriesForASIN(MarketplaceWebServiceProducts_Interface $service, $request)
	{
		try
		{
			$response = $service->GetProductCategoriesForASIN($request);

			$this->amazon_mws->format_output($response, 'GetProductCategoriesForASINResult|Self', 'ProductCategoriesForASIN');

			$this->amazon_mws->show_ouput(TRUE);


		}
		catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}

	}

	function invokeGetMyPriceForSKU(MarketplaceWebServiceProducts_Interface $service, $request)
	{
		try
		{
			$response = $service->GetMyPriceForSKU($request);

			$this->amazon_mws->format_output($response, 'GetMyPriceForSKUResponse|GetMyPriceForSKUResult', 'PriceForSKU');

			$this->amazon_mws->show_ouput(TRUE);


		}
		catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}

	}

	function invokeGetMyPriceForASIN(MarketplaceWebServiceProducts_Interface $service, $request)
	{
		try
		{
			$response = $service->GetMyPriceForASIN($request);

			$this->amazon_mws->format_output($response, 'GetMyPriceForASINResponse|GetMyPriceForASINResult', 'PriceForASIN');

			$this->amazon_mws->show_ouput(TRUE);


		}
		catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}

	}

	function invokeGetMatchingProduct(MarketplaceWebServiceProducts_Interface $service, $request)
	{
		try
		{
			$response = $service->GetMatchingProduct($request);

			$this->amazon_mws->format_output($response, 'GetMatchingProductResponse|GetMatchingProductResult', 'GetMatchingProductResult');

			$this->amazon_mws->show_ouput(TRUE);


		}
		catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}

	}

	function invokeGetMatchingProductForId(MarketplaceWebServiceProducts_Interface $service, $request)
	{

		try
		{
			$response = $service->GetMatchingProductForId($request);

			$this->amazon_mws->format_output($response, 'GetMatchingProductForIdResponse|GetMatchingProductForIdResult', 'GetMatchingProductForIdResult');

			$this->amazon_mws->show_ouput(TRUE);


		}
		catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}
		
	}

	function invokeGetLowestOfferListingsForSKU(MarketplaceWebServiceProducts_Interface $service, $request)
	{
		try
		{
			$response = $service->GetLowestOfferListingsForSKU($request);
		
			$this->amazon_mws->format_output($response, 'GetLowestOfferListingsForSKUResponse|GetLowestOfferListingsForSKUResult', 'GetLowestOfferListingsForSKUResult');
		
			$this->amazon_mws->show_ouput(TRUE);
		
		
		}
		catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}
		
	}
	
	function invokeGetLowestOfferListingsForASIN(MarketplaceWebServiceProducts_Interface $service, $request)
	{
		try
		{
			$response = $service->GetLowestOfferListingsForASIN($request);
	
			$this->amazon_mws->format_output($response, 'GetLowestOfferListingsForASINResponse|GetLowestOfferListingsForASINResult', 'GetLowestOfferListingsForASINResult');
	
			$this->amazon_mws->show_ouput(TRUE);
	
	
		}
		catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}
	
	}
	
	function invokeGetCompetitivePricingForSKU(MarketplaceWebServiceProducts_Interface $service, $request)
	{
		try
		{
			$response = $service->GetCompetitivePricingForSKU($request);
		
			$this->amazon_mws->format_output($response, 'GetCompetitivePricingForSKUResponse|GetCompetitivePricingForSKUResult', 'GetCompetitivePricingForSKUResult');
		
			$this->amazon_mws->show_ouput(TRUE);
		
		
		}
		catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}
		
	}
	
	function invokeGetCompetitivePricingForASIN(MarketplaceWebServiceProducts_Interface $service, $request)
	{
		try
		{
			$response = $service->GetCompetitivePricingForASIN($request);
	
			$this->amazon_mws->format_output($response, 'GetCompetitivePricingForASINResponse|GetCompetitivePricingForASINResult', 'GetCompetitivePricingForASINResult');
	
			$this->amazon_mws->show_ouput(TRUE);
	
	
		}
		catch (MarketplaceWebServiceProducts_Exception $ex)
		{
			$this->amazon_mws->format_error($ex->getXML());
			$this->amazon_mws->show_ouput(FALSE);
		}
	
	}
	
	

}